﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDM3Server;
using System.Web;
using NLog;


namespace Automaton.ServiceLibrary
{
    public static class PLC2414
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        public static SDM3 _sdm3;
        public const string Category = "PLC";
        static PLC2414()
        {
            Init();
        }

        public static Contracts.Results Init()
        {
            logger.Debug("Init()", Category);
            Contracts.Results result = new Contracts.Results();
            try
            {
                _sdm3 = new SDM3();
            }
            catch (Exception excp)
            {
                result.bResult = false;
                result.ExceptionMessage = excp.ToString();
                logger.FatalException(Category, excp);
                throw;
            }
            return result;
        }
        

        public static Contracts.Results _IsResponding()
        {
            logger.Debug("_IsResponding())", Category);
            Contracts.Results result = new Contracts.Results();
            try
            {
                if (IsResponding())
                    result.bResult = true;
                else
                {
                    _sdm3 = new SDM3();
                    result.bResult = IsResponding();
                }

                
            }
            catch (Exception excp)
            {
                result.bResult = false;
                result.ExceptionMessage = excp.ToString();
                logger.FatalException(Category, excp);
                throw;
            }
            return result;
        }

        public static bool IsResponding()
        {
            //logger.Debug("IsResponding()", Category);
            bool result = false;
            result = _sdm3.IsResponding();
            return result;
        }
      
        public static bool _sendX10(string command)
        {
            logger.Debug("_sendX10(string " + (string.IsNullOrEmpty(command) ? command : "" + ")"), Category);
            bool result = false;
            try
            {
                if (IsResponding())
                    result = _sdm3.SendX10(command);
                else
                    result = false;
            }
            catch (Exception excp)
            {
                logger.FatalException(Category, excp);
                result = false;
                throw;
            }
            return result;
        }

        public static Contracts.Results sendX10(Contracts.HouseCodes code, int Address, Contracts.X10Commands command)
        {
            logger.Debug("sendX10(HouseCodes " + code.ToString() + ", int " + Address.ToString() + ", X10Commands " + command.ToString(), Category);
            Contracts.Results result = new Contracts.Results();
            try
            {
                if (IsResponding())
                {
                    if (_sendX10(code.ToString() + Address.ToString()))
                    {
                        if (_sendX10(code.ToString() + command.ToString()))
                        {
                            result.bResult = true;
                        }
                        else
                        {
                            result.bResult = false;
                            result.ExceptionMessage = "Action Failed";
                        }
                    }
                    else
                    {
                        result.bResult = false;
                        result.ExceptionMessage = "Address Failed";
                    }
                }
                else
                    initNotRespondingResult(ref result);
            }
            catch (Exception excp)
            {
                result.ExceptionMessage = excp.ToString();
                result.bResult = false;
                logger.FatalException(Category, excp);
                throw;
            }
            return result;
        }

        public static void initNotRespondingResult(ref Contracts.Results results)
        {
            logger.Debug("initNotRespondingResult(ref Results " + results.ToString(), Category);
            results.bResult = false;
            results.ExceptionMessage = "SDM not responding!";
        }

    }
}
